// ==UserScript==
// @name         允许复制网页所有内容
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  解除网页内容复制限制，作者：ddjyx
// @author       ddjyx
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 移除阻止默认行为的事件监听器
    function removeCopyRestrictions() {
        const eventNames = ['copy', 'cut', 'paste', 'contextmenu', 'selectstart'];
        document.addEventListener('mouseover', (event) => {
            eventNames.forEach(eventName => {
                let target = event.target;
                while (target && target !== document) {
                    if (target[eventName]) {
                        try {
                            target.removeEventListener(eventName, target[eventName]);
                            delete target[eventName];
                        } catch (e) {}
                    }
                    target = target.parentNode;
                }
            });
        }, true);
    }

    // 禁用禁止选择文本的样式
    function disableUserSelectStyles() {
        const css = '* {-webkit-user-select: auto !important; -moz-user-select: auto !important; -ms-user-select: auto !important; user-select: auto !important;}';
        const styleSheet = document.createElement("style");
        styleSheet.type = "text/css";
        styleSheet.innerText = css;
        document.head.appendChild(styleSheet);
    }

    // 解决iframe中的内容不可复制问题
    function handleIFrame() {
        const iframes = document.getElementsByTagName('iframe');
        for (let i = 0; i < iframes.length; i++) {
            iframes[i].contentWindow.document.body.onselectstart = () => true;
            iframes[i].contentWindow.document.body.oncopy = (e) => {
                e.stopPropagation();
                return true;
            };
        }
    }

    removeCopyRestrictions();
    disableUserSelectStyles();
    handleIFrame();

})();